/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbRedsTool.cpp		*/
/********************************************************/

#include <getopt.h>

#include "GlbRtbMems.h"

void GlbRtbRedsSelect(char* pszHost, char* pszID)
{
	UINT uiCnt = 0;
	GlbNet::CGlbNet CNet;
	GLBRDCON_P pstCon = NULL;
	GLBRDREP_P pstVal = NULL;
	char* pszPort = CNet.GlbNetUrl(pszHost);
	char* pszKey[] = {
		GLB_RD_BGTM,
		GLB_RD_EDTM,
		GLB_RD_BROW,
		GLB_RD_OSNM,
		GLB_RD_LOCA,
		GLB_RD_MDTY,
		GLB_RD_MEDI,
		GLB_RD_PRIO,
		GLB_RD_VOLU,
		GLB_RD_PRIC,
		GLB_RD_CRUL,
		GLB_RD_SCRN,
		GLB_RD_TYPE
	};

	if (NULL != (pstCon = GLB_RD_RCNT(pszHost, atoi(pszPort)))) {
		if (NULL != (pstVal = (GLBRDREP_P)GLB_RD_RCMD(pstCon, "HMGET %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
			pszID,
			pszKey[0],
			pszKey[1],
			pszKey[2],
			pszKey[3],
			pszKey[4],
			pszKey[5],
			pszKey[6],
			pszKey[7],
			pszKey[8],
			pszKey[9],
			pszKey[10],
			pszKey[11],
			pszKey[12]
		))) {
			for (uiCnt = 0; uiCnt < GLB_RD_RTTL(pstVal); uiCnt ++) {
				GLB_PRINT("%s: %s\n", pszKey[uiCnt], GLB_RD_RVAL(pstVal, uiCnt));
			}
			GLB_RD_RFRO(pstVal);
		}
		GLB_RD_RFRE(pstCon);
	}

	return;
}

int main(int argc, char* argv[])
{
 	int iIndex = 0;
	int iReturn = 0;
	char* pszID = NULL;
	char szHost[GLB_URL] = "127.0.0.1:6379";
	struct option stOpt[] = {
		{"host", 1, 0, 'u'},
		{"print", 1, 0, 'p'},
		{"version", 0, 0, 'v'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0},
	};

	while (GLB_FAILURE != (iReturn = getopt_long(argc, argv, "u:p:vh", stOpt, &iIndex))) {
		switch (iReturn) {
			case 'u':
				strncpy(szHost, optarg, GLB_URL);
				break;
			case 'p':
				pszID = optarg;
				break;
			case 'v':
				GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
				GLB_PRINT("GlbRtbRedsTool Build on %s %s.\n", __DATE__, __TIME__);
				return GLB_SUCCESS;
			case 'h':
				GLB_PRINT("Usage: %s [options]\n", argv[0]);
				GLB_PRINT("Valid options are:\n");
				GLB_PRINT("[0] -h or --host:       set redis host\n");
				GLB_PRINT("[0] -p or --print:      print advs by id\n");
				GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
				GLB_PRINT("GlbRtbRedsTool Build on %s %s.\n", __DATE__, __TIME__);
				return GLB_SUCCESS;
			default:
				return GLB_SUCCESS;
		}
	}

	if (NULL != pszID) {
		GlbRtbRedsSelect(szHost, pszID);
	}
	else {
		GLB_PRINT("ID error\n");
	}

	return GLB_SUCCESS;
}
